package com.miraclengine.karma;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.miraclengine.framework.Game;
import com.miraclengine.framework.Input.TouchEvent;
import com.miraclengine.framework.gl.Camera2D;
import com.miraclengine.framework.gl.SpriteBatcher;
import com.miraclengine.framework.impl.GLScreen;
import com.miraclengine.framework.math.Vector2;
import com.miraclengine.utils.GlobalStatic;
import com.miraclengine.utils.ProgressAsyncTask;
import com.miraclengine.xml.DataTable;
import com.miraclengine.xml.XmlHttpClientHelper;

public class KarmaLogoScreen extends GLScreen {
	
	Camera2D guiCam;
	SpriteBatcher batcher;
	
	Vector2 touchPoint;
	
	float counter = 0;
	
	boolean isLoading = true;

	public KarmaLogoScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
		guiCam = new Camera2D(glGraphics, 1184, 720);
		batcher = new SpriteBatcher(glGraphics, 100);
		
		touchPoint = new Vector2();
		
		//new ProgressAsyncTask(mCallBack, new Handler(Looper.getMainLooper())).execute();
		
		//new MyTask().execute();
		
		
		//t.start();
	}	
	
	Handler h = new Handler(Looper.getMainLooper()) {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what) {
			case 10:
				Log.d("TAG", "왜 이러는 걸까요?");
				break;
			}
		}
	};
	
	Thread t = new Thread(new Runnable() {
		public void run() {
			Message message = Message.obtain(h, 10);
			h.sendMessage(message);
		}
	});
	
	class MyTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
		
	}
	
	private void doMatchData(DataTable dt) {
		dt.d();
		isLoading = false;
	}
	
	ProgressAsyncTask.CallBack mCallBack = new ProgressAsyncTask.CallBack() {
		
		@Override
		public void onPostExecute(DataTable dt) {
			// TODO Auto-generated method stub
			if(GlobalStatic.LAST_EXCUTE_RESULT_CODE.equals("1")) {
				doMatchData(dt);
			}
		}
		
		@Override
		public DataTable doInBackground(String... datas) {
			// TODO Auto-generated method stub
			
			String preUrl = "http://vinchi.miraclengine.com/ServerKarma/proc.php?";
			
			ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("mode", "get"));
			params.add(new BasicNameValuePair("id", "00001"));
			
			DataTable dt = XmlHttpClientHelper.getInstance().execute(preUrl, XmlHttpClientHelper.METHOD.GET, params);
			
			return dt;
		}
	};

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		for(TouchEvent event : touchEvents) {
			if(event.type == TouchEvent.TOUCH_UP) {
				touchPoint.set(event.x, event.y);
				guiCam.touchToWorld(touchPoint);
				
				Log.d("TAG", "x : " + touchPoint.x + ", y : " + touchPoint.y);
			}
		}

		counter += deltaTime;
		
		//Log.d("TAG", "counter : " + counter);
		if(counter > 4) {
			game.setScreen(new KarmaMenuScreen(game));
		}
	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.image_data);
        batcher.drawSprite(592, 360, 1184, 720, Assets.kLogoRegion);
        batcher.endBatch();
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
